package com.yidu.hbatsams.action.AtsBaseSport;

import com.yidu.hbatsams.domain.AtsBaseSport;
import com.yidu.hbatsams.domain.AtsBaseSportItem;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.AtsBaseSportItemService;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.utils.AtsBaseItemTree;
import com.yidu.hbatsams.utils.Message;
import com.yidu.hbatsams.utils.redis.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/BaseAction")
public class AtsBaseSportAction {

    Logger logger= LoggerFactory.getLogger(getClass());


    @Resource
    AtsBaseSportService bsService;
    @Resource
    AtsBaseSportItemService atsBaseSportItemService;

    @Resource
    RedisUtil redisUtil;



    /**
     * 查询所有及分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAll")
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize,String sportName){
        //调用map方法
        Map<String,Object> map = new HashMap<>();
        //定义总行数
        int count = bsService.count();
        //调用项目管理查询所有方法
        List<AtsBaseSport> list =bsService.selectAll(pageNum,pageSize,sportName);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }


    /**
     * 修改状态
     * @param id
     * @param
     * @return
     */
    @RequestMapping("/updateId")
    @ResponseBody
    public int updateId(String id) {
        redisUtil.tj("BaseItem");
        return bsService.updateId(Integer.valueOf(id));
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public AtsBaseSport findById(Integer id){
        AtsBaseSport bs = bsService.findById(id);
        return bs;
    }

    /**
     * 修改增加
     * @param atsBaseSport
     * @return
     */
    @ResponseBody
    @RequestMapping("/addAndUpdate")
    public Message updateByPrimaryKey(HttpSession session,AtsBaseSport atsBaseSport, String itemIds){
        //定义变量
        int rows=0;
        //调用message类
        Message msg = new Message();
        //判断项目ID为空
        if (atsBaseSport.getSportId()==null){
            //调用项目管理增加方法
            rows = bsService.insert(atsBaseSport,itemIds);
            //判断项目ID不为空
        }else if(atsBaseSport.getSportId()!=null&&atsBaseSport.getSportId()>0){
            //查询到该项目所有指标
            List<AtsBaseSportItem> atsBaseSportItems = atsBaseSportItemService.selectId(atsBaseSport.getSportId());
            //定义为空的字符串
            String ids = "";
            //迭代器
            for (Iterator<AtsBaseSportItem> iterator = atsBaseSportItems.iterator(); iterator.hasNext(); ) {
                AtsBaseSportItem next =  iterator.next();
                //将查到的ID全部分割存入字符串中
                ids += next.getSiId().toString()+",";
            }

            //定义变量
            int count = 0;
            //将字符串中的id存入数组
            String siid[] = ids.split(",");
            //将数组中的id 存入int数组中
            int[] array = new int[siid.length];
            //调用list
            List<AtsBaseSportItem> atsBaseSportItems1 = new ArrayList<>();
            //判断数组长度大于0
            if(siid.length > 0) {
                //读取数组长度，循环保存
                for (int i = 0; i < siid.length; i++) {
                    //删除在新增，存入int 数组
                    array[i] = Integer.parseInt(siid[i]);
                }
            }
            //调用项目管理修改方法
            rows = bsService.updateByPrimaryKey(atsBaseSport,array,itemIds);

        }
        //if判断messger
        if (rows>0){
            msg.setNum(1);
            msg.setMsg("操作成功");
        }else{
            msg.setNum(0);
            msg.setMsg("操作失败");
        }
       //返回msg
        return msg;
    }


    /**
     * 下拉树
     * @return 返回一个集合
     */
    @ResponseBody
    @RequestMapping("/treeNode")
    public List<AtsBaseItemTree> findTreeNode(String sportId){
        //判断项目id不为空
        if (sportId != null ) {
            //定义一个为空的字符串
            String ids = "";
            //调用关联表的根据id查询方法
            List<AtsBaseSportItem> atsBaseSportItems = atsBaseSportItemService.selectId(Integer.parseInt(sportId));
            //foreach
            for (AtsBaseSportItem a : atsBaseSportItems) {
                //将指标id分割存入字符串
                ids += a.getItemId().toString() + ",";
            }
            //将字符串id分割存入数组
            String[] id = ids.split(",");
            //返回项目管理树状方法跟数组
            return tree(bsService.findTreeNode(), id);
        }else{
            //返回项目管理树状方法，null
            return tree(bsService.findTreeNode(),null);
        }

    }


    /**
     * 树状应用
     * @param a
     * @param at
     * @return
     */
    public  List<AtsBaseItemTree> tree(List<AtsBaseItemTree> a,String[] at){

        List<AtsBaseItemTree> list = new ArrayList<>();
            for (AtsBaseItemTree ats : a) {
                ats.setValue(ats.getId());
                if (at != null) {
                    for (int i = 0; i < at.length; i++) {
                        if (at[i].equals(ats.getValue())) {
                            ats.setChecked(true);
                        }
                        if (ats.getChildren().size() > 0 && ats.getChildren() != null) {
                            ats.setData(ats.getChildren());
                            tree(ats.getData(), at);
                        }
                    }
                }else{
                    if (ats.getChildren().size() > 0 && ats.getChildren() != null) {
                        ats.setData(ats.getChildren());
                        tree(ats.getData(), at);
                    }
                }
                list.add(ats);
            }
            return list;
        
    }

}
